package com.example.xmpic.ui.adapter;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.common.utils.LiveDataBus;
import com.example.xmpic.R;
import com.example.xmpic.R2;
import com.example.xmpic.support.models.FunDevice;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2020-02-18.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：局域网内摄像头列表
 */
public class LocalNetIPCAdapter extends RecyclerView.Adapter<LocalNetIPCAdapter.ItemHolder> {

    private Context mContext;
    private List<FunDevice> mLists;

    public static final String ITEM_CLICK_IPC = "item_click_ipc";

    public LocalNetIPCAdapter(Context context) {
        this.mContext = context;
    }

    public void setLists(List<FunDevice> mLists) {
        this.mLists = mLists;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_local_ipc, viewGroup, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        FunDevice device = mLists.get(i);
        itemHolder.subMonitor.setImageResource(device.getDevType().getDrawableResId());
        itemHolder.monitorName.setText(device.getDevName());
        itemHolder.monitorSn.setText(device.getDevSn());
        itemHolder.monitorIp.setText(device.getDevIP());
        if (device.devStatus.getSatusId() == 1){
            itemHolder.mStatus.setImageResource(R.drawable.device_status_normal);
        }else {
            itemHolder.mStatus.setImageResource(R.drawable.device_status_off_line);
        }
        itemHolder.itemView.setOnClickListener(view -> LiveDataBus.get().with(ITEM_CLICK_IPC).setValue(i));
    }

    @Override
    public int getItemCount() {
        return mLists == null ? 0 : mLists.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.sub_monitor)
        ImageView subMonitor;
        @BindView(R2.id.monitor_name)
        TextView monitorName;
        @BindView(R2.id.monitor_sn)
        TextView monitorSn;
        @BindView(R2.id.monitor_ip)
        TextView monitorIp;
        @BindView(R2.id.monitor_status)
        ImageView mStatus;

        ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
